package org.jeecg.module.action;


import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.card.entity.CardPhoneUser;
import org.jeecg.modules.demo.card.service.ICardPhoneUserService;
import org.jeecg.modules.demo.card.service.impl.CardPhoneUserServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author: xiaYZ 2024/3/7
 * @version: 1.0
 */
@RestController
@RequestMapping("testAction")
public class TestAction {


    @Resource
    private RedisUtil redisUtil;


    @Resource
    private ICardPhoneUserService cardPhoneUserService;


    @GetMapping("testAction22")
    public Result<String> testAction(String message) {
        return Result.ok("testAction" + message);
    }

    @GetMapping("testException")
    public Result<String> testException() {

        int i = 1 / 0;
        return Result.error("testException");
    }

    @GetMapping("findCardPhoneUser")
    public Result<JSONObject> findCardPhoneUser(Long userId) {
        Result<JSONObject> result = new Result<>();
        CardPhoneUser phoneUser = cardPhoneUserService.getById(userId);
        if (null != phoneUser) {
            String username = phoneUser.getUserName();
            String syspassword = phoneUser.getPassword();
            // 获取用户部门信息
            JSONObject obj = new JSONObject(new LinkedHashMap<>());

            //1.生成token
            String token = JwtUtil.sign(username, syspassword);
            // 设置token缓存有效时间
            redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
            redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME * 2 / 1000);


            redisUtil.set(CacheConstant.SYS_USERS_CACHE + "::" + username, phoneUser);
            redisUtil.expire(CacheConstant.SYS_USERS_CACHE + "::" + username, JwtUtil.EXPIRE_TIME * 2 / 1000);
            obj.put("token", token);


            //3.设置登录用户信息
            obj.put("userInfo", phoneUser);

            result.setResult(obj);
            result.success("登录成功");
            return result;
        }
        return Result.error("用户不存在:" + phoneUser);
    }

    @GetMapping("testAction")
    public String testAction() {
        return "testAction";
    }


    /**
     * 登录失败超出次数5 返回true
     *
     * @param username
     * @return
     */
    private boolean isLoginFailOvertimes(String username) {
        String key = CommonConstant.LOGIN_FAIL + username;
        Object failTime = redisUtil.get(key);
        if (failTime != null) {
            Integer val = Integer.parseInt(failTime.toString());
            if (val > 5) {
                return true;
            }
        }
        return false;
    }
}
